/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package earthdawnnet.server.shareable.charactersheet;

import earthdawnnet.client.Persister;
import earthdawnnet.server.shareable.Shareable;
import java.io.Serializable;
import javax.swing.JFrame;

/**
 *
 * @author Andreas Franek
 */
public class CharacterSheet implements Serializable, Shareable
{

    public void setPlayerName(String playerName)
    {
        this.playerName = playerName;
    }

    public void setDiscipline(Discipline discipline)
    {
        this.discipline = discipline;
    }

    public void setCircle(int circle)
    {
        this.circle = circle;
    }
    
    // <editor-fold defaultstate="collapsed" desc="Race, class, general">
    public static enum Race {Elf, Human, Obsidian, Ork, Troll, TSkrang, Windling, Dwarf};
    public static enum Discipline {Thief, Elementarist, Nethermancer, Illusionist, Warrior, SkyRaider, Wizard, Archer, Swordmaster, Beastmaster, Cavalryman, Troubadour, Weaponsmith};
    public static enum Sex {None, Female, Male, Both};

    //General
    private String name;
    private String playerName;
    private Discipline discipline;
    private int circle;
    private Race race;
    private Sex sex;
    private int age;
    private String hairColor;
    private String eyeColor;
    private int height;
    private int weight;
    //</editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Racial attribute modifiers">
    public static final int[][] racialAttrMod = {
        {
            2, 0, -2, 1, 1, 1
        },
        {
            0, 0, 0, 0, 0, 0
        },
        {
            -2, 6, 4, -1, 0, -1
        },
        {
            -1, 3, 1, 0, -1, -1
        },
        {
            0, 4, 2, -1, 1, 0
        },
        {
            1, 0, 1, 0, 0, 1
        },
        {
            1, -4, -3, 1, 0, 2
        },
        {
            0, 2, 3, 0, 0, -1
        }
    };
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Attributes">
    //Attributes
    private int[] attributes = new int[6];
    public static enum Attribute {Dexterity, Strength, Toughness, Perception, Willpower, Charisma};
    
    private int[] raisedAttr = new int[6];
    public int getAttribute(Attribute a)
    {
        return attributes[a.ordinal()] + raisedAttr[a.ordinal()] + racialAttrMod[race.ordinal()][a.ordinal()];
    }
    
    public void setAttribute(Attribute a, int value)
    {
        attributes[a.ordinal()] = value;
    }
    
    public int getStepOfAttribute(Attribute a)
    {
        return (getAttribute(a) + 5)/3;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Characteristics">
    //Characteristics
    public enum Resistance {Physical, Magical, Social};
    
    public int getResistance(Resistance r)
    {
        int res = 0;
        switch(r)
        {
            case Physical:
                res = getResistanceForAttributeValue(getAttribute(Attribute.Dexterity));
                break;
            case Magical:
                res = getResistanceForAttributeValue(getAttribute(Attribute.Perception));
                break;
            case Social:
                res = getResistanceForAttributeValue(getAttribute(Attribute.Charisma));
                break;
            default:
                throw new IllegalArgumentException("invalid resistance type");
        }
        return res;
    }
    
    private int getResistanceForAttributeValue(int attrValue)
    {
        int result = 2;
        int mod = (attrValue%7);
        result += mod*3;
        if(mod > 1) result += 1;
        else if (mod > 3) result += 2;
        return result;
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Persisting">
    public static final Persister<CharacterSheet> charPersister = new Persister<CharacterSheet>();
    public static final String charFileExtension = ".char";
    
    public static void save(CharacterSheet c)
    {
        charPersister.save(c, c.getName()+charFileExtension);
    }
    
    public static CharacterSheet load(String characterName)
    {
        return load(characterName+charFileExtension);
    }
    
    public void save()
    {
        save(this);
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Shareable">
    public JFrame getVisualRepresentation()
    {
        return CharacterView.startup(this);
    }
    //</editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Getters & Setters">
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
    
    public void setRace(Race race)
    {
        this.race =race; 
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public void setSex(Sex sex)
    {
        this.sex = sex;
    }

    public void setWeight(int weight)
    {
        this.weight = weight;
    }

    public void setHeight(int height)
    {
        this.height = height;
    }

    public void setHairColor(String hairColor)
    {
        this.hairColor = hairColor;
    }

    public void setEyeColor(String eyeColor)
    {
        this.eyeColor = eyeColor;
    }
    
    public void setDexterity(int value)
    {
        setAttribute(Attribute.Dexterity, value);
    }

    public void setStrength(int value)
    {
        setAttribute(Attribute.Strength, value);
    }

    public void setToughness(int value)
    {
        setAttribute(Attribute.Toughness, value);
    }

    public void setPerception(int value)
    {
        setAttribute(Attribute.Perception, value);
    }

    public void setWillpower(int value)
    {
        setAttribute(Attribute.Willpower, value);
    }

    public void setCharisma(int value)
    {
        setAttribute(Attribute.Charisma, value);
    }
    // </editor-fold>
}
